import scrapy
from scrapy_test04_dangdang.items import ScrapyTest04DangdangItem


class DangSpider(scrapy.Spider):
    name = 'dang'
    allowed_domains = ['http://category.dangdang.com/cp01.01.02.00.00.00.html']
    start_urls = ['http://category.dangdang.com/cp01.01.02.00.00.00.html']

    def parse(self, response):
        #pipeline 下载数据
        #items    定义数据结构
        #src = '//ul[@id="component_59"]/li//img/@src'
        #alt = '//ul[@id="component_59"]/li//img/@alt'
        #price  = '//ul[@id="component_59"]/li//p[@class = "price"]/span[1]/text()'
        #所有的select的对象都可以再次调用XPath方法
        li_list = response.xpath('//ul[@id="component_59"]/li')
        for li in li_list:
            src = li.xpath('.//img/@data-original').extract_first()
            if src:
                src=src
            else:
                src = li.xpath('.//img/@src').extract_first()
            name = li.xpath('.//img/@alt').extract_first()
            price = li.xpath('.//p[@class = "price"]/span[1]/text()').extract_first()
            book = ScrapyTest04DangdangItem(src = src,name = name,price = price)
            #交给pipeline下载

            #获取一个book就将book交给pipelines（迭代器）
            yield book